<?php

/**
 * home actions.
 *
 * @package    dnevnik
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class homeActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  }

  public function executeOprogramu(sfWebRequest $request)
  {
  }

  public function executeKorisnici(sfWebRequest $request)
  {
  }

  public function executeSkole(sfWebRequest $request)
  {
  }

  public function executeNovosti(sfWebRequest $request)
  {
  }

  public function executePress(sfWebRequest $request)
  {
  }

  public function executeKontakt(sfWebRequest $request)
  {
     if ($this->getRequest()->getMethod() == sfRequest::POST)
     {
        $msg =
            'Ime:   '.$_POST['name'].'<br />
            Telefon:   '.$_POST['tel'].'<br />
            Email:   '.$_POST['email'].'<br />
            Kontaktirajte:   '.$_POST['subject'].'<br />
            IP:   '.$_SERVER['REMOTE_ADDR'].'<br /><br />
            Tema poruke:   '.$_POST['tema'].'<br />
            Poruka:<br /><br />

            '.nl2br($_POST['message']).'

            ';
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=utf-8\r\n";
        $headers .= "From: podrska@skolskidnevnik.rs\r\n";
        $headers .= "Reply-To: podrska@skolskidnevnik.rs\r\n";
        mail('info@skolskidnevnik.rs', 'Poruka sa www.skolskidnevnik.rs', $msg, $headers);

        $this->success='<h1>Hvala. Poruka je poslata!</h1>';

        $this->css='<style type="text/css">#contact-form{display:none;}</style>';
     }
  }


  // login

  private function authenticate($profil)
  {
      $profil->setLastActiv(time());
      $profil->save();
      $this->getUser()->setAuthenticated(true);
      $this->getUser()->setAttribute("id", $profil->getId());
      $this->getUser()->setAttribute("tip", $profil->getTip());

      if($profil->getTip() === ProfilPeer::$tip['roditelj'])
      {
        $this->getUser()->addCredential("roditelj");
        $roditelj = DbFinder::from('Roditelj')->findPk($profil->getId());
        if(!($roditelj->getClanarina()))
        {
          $roditelj->setClanarina(date('Y-n-d', time() + 7 * 24 * 60 * 60));
          $roditelj->save();
        }
      }

      else if($profil->getTip() === ProfilPeer::$tip['ucenik'])
      {
        $this->getUser()->addCredential("ucenik");
      }
  }

  /**
  *@desc klasicna autentifikacija, provera username-a i passworda
  */
  public function executeLogin($id)
  {
     $disabled_skolas = array();
      if ($this->getRequest()->getMethod() != sfRequest::POST)
      {
          // display the form
          $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
      }
      else
      {
          // handle the form submission
          $username = $this->getRequestParameter('username');
          $password = $this->getRequestParameter('password');

          $profil = DbFinder::from('Profil')->where('Username', $username)->where('Password', md5($password))->findOne();
          if($profil)
          {
             $tips = ProfilPeer::getTips();
             $tip = ucwords($tips[$profil->getTip()]);
             if($tip == 'Profesor' or $tip == 'Roditelj' or $tip == 'Ucenik')
             {
               $skola_id_1 = DbFinder::from('Profil')->join($tip)->select('SkolaId')->withColumn($tip.'.SkolaId', 'SkolaId')->findPk($profil->getId());
               $skola_ids = array($skola_id_1);
             }

             else if($tip == 'Upravnik')
             {
                $skola_ids = DbFinder::from('Profil')->join('Upravnik')->join('UpravnikSkola')->select('SkolaId')->withColumn('UpravnikSkola.SkolaId', 'SkolaId')->where('Profil.Id', $profil->getId())->find();
             }

             else if($tip == 'Operater')
             {
                $skola_ids = DbFinder::from('Profil')->join('Operater')->join('OperaterRazred')->join('Razred')->select('SkolaId')->withColumn('Razred.SkolaId', 'SkolaId')->where('Profil.Id', $profil->getId())->groupBy('Razred.SkolaId')->find();
             }
             foreach($skola_ids as $skola_id)
               if(in_array($skola_id, $disabled_skolas))
                 die('Greska pri logiranju!!!');


              $logiranje = new Logiranje();
              $logiranje->setProfilId($profil->getId());
              $logiranje->setTip(LogiranjePeer::$tip['login']);
              $logiranje->save();

              if($profil->getTip() === ProfilPeer::$tip['roditelj'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = "/panel.php/user/login";
                  die(Functions::postRedirect($data, $uri));
              }

              else if($profil->getTip() === ProfilPeer::$tip['administrator'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = "/backend.php/user/login";
                  die(Functions::postRedirect($data, $uri));
              }

              else if($profil->getTip() === ProfilPeer::$tip['operater'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = "/administracija.php/user/login";
                  die(Functions::postRedirect($data, $uri));
              }

              else if($profil->getTip() === ProfilPeer::$tip['profesor'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = '/skola.php/user/login';
                  die(Functions::postRedirect($data, $uri));
              }

              else if($profil->getTip() === ProfilPeer::$tip['upravnik'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = "/uprava.php/user/login";
                  die(Functions::postRedirect($data, $uri));
              }
          }
          $this->redirect('home/index');
          return sfView::NONE;
      }
  }

  /**
  *@desc logout i redirect na homepage
  */
  public function executeLogout(sfWebRequest $request)
  {
      if($this->getUser()->getAttribute('id'))
      {
        $logiranje = new Logiranje();
        $logiranje->setProfilId($this->getUser()->getAttribute('id'));
        $logiranje->setTip(LogiranjePeer::$tip['logout']);
        $logiranje->save();
      }

      $this->getUser()->setAuthenticated(false);
      $this->getUser()->clearCredentials();
      $this->getUser()->getAttributeHolder()->clear();
      $this->redirect('@homepage');
      return sfView::NONE;
  }
}
